<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tallgy.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="关于 属性原型的一个问题​        先说问题和总结 1234567const obj &#x3D; &amp;#123;  a: 1,&amp;#125;obj.__proto__.x &#x3D; 1&#x2F;&#x2F; 可以这样直接访问x.log  ​        总结： ​        因为全局的上下文的 proto 也是 Object.prototype。所以我们修改了 obj.proto 实际上也修改了 Object.proto">
<meta property="og:type" content="article">
<meta property="og:title" content="Object.__proto__">
<meta property="og:url" content="http://tallgy.gitee.io/2022/03/03/JavaScript/%E5%AF%B9%E8%B1%A1/Object-proto/index.html">
<meta property="og:site_name" content="tallgy&#39;s blog">
<meta property="og:description" content="关于 属性原型的一个问题​        先说问题和总结 1234567const obj &#x3D; &amp;#123;  a: 1,&amp;#125;obj.__proto__.x &#x3D; 1&#x2F;&#x2F; 可以这样直接访问x.log  ​        总结： ​        因为全局的上下文的 proto 也是 Object.prototype。所以我们修改了 obj.proto 实际上也修改了 Object.proto">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-03-03T03:07:45.000Z">
<meta property="article:modified_time" content="2023-06-25T08:57:29.338Z">
<meta property="article:author" content="tallgy">
<meta property="article:tag" content="随笔">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://tallgy.gitee.io/2022/03/03/JavaScript/%E5%AF%B9%E8%B1%A1/Object-proto/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://tallgy.gitee.io/2022/03/03/JavaScript/%E5%AF%B9%E8%B1%A1/Object-proto/","path":"2022/03/03/JavaScript/对象/Object-proto/","title":"Object.__proto__"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Object.__proto__ | tallgy's blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">tallgy's blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%B3%E4%BA%8E-%E5%B1%9E%E6%80%A7%E5%8E%9F%E5%9E%8B%E7%9A%84%E4%B8%80%E4%B8%AA%E9%97%AE%E9%A2%98"><span class="nav-number">1.</span> <span class="nav-text">关于 属性原型的一个问题</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tallgy</p>
  <div class="site-description" itemprop="description">只是一个知识的搬运工</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">107</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2022/03/03/JavaScript/%E5%AF%B9%E8%B1%A1/Object-proto/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Object.__proto__
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-03-03 11:07:45" itemprop="dateCreated datePublished" datetime="2022-03-03T11:07:45+08:00">2022-03-03</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/JavaScript/" itemprop="url" rel="index"><span itemprop="name">JavaScript</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/JavaScript/%E9%9A%8F%E7%AC%94/" itemprop="url" rel="index"><span itemprop="name">随笔</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="关于-属性原型的一个问题"><a href="#关于-属性原型的一个问题" class="headerlink" title="关于 属性原型的一个问题"></a>关于 属性原型的一个问题</h1><p>​        先说问题和总结</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">const obj = &#123;</span><br><span class="line">  a: 1,</span><br><span class="line">&#125;</span><br><span class="line">obj.__proto__.x = 1</span><br><span class="line"></span><br><span class="line">// 可以这样直接访问</span><br><span class="line">x.log</span><br></pre></td></tr></table></figure>

<p>​        总结：</p>
<p>​        因为全局的上下文的 proto 也是 Object.prototype。所以我们修改了 obj.proto 实际上也修改了 Object.prototype。所以，不要随意修改高层的原型，容易出大问题啊。</p>
<p>​        首先我们可以知道，一个对象，存在一个 prototype 原型，然后在创建一个实例的时候，会将其创建为一个 proto 的内部属性进行赋值。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">const obj = &#123;</span><br><span class="line">  a: 1,</span><br><span class="line">&#125;</span><br><span class="line">obj.__proto__.x = 1</span><br></pre></td></tr></table></figure>

<p>​        如果上面这样写，就是说，设置了 obj 的 proto 属性，然后就会在 Object.prototype 里面创建上这个属性，因为 proto 就是引用的 prototype，对于一个对象，他的引用就是 Object 的 prototype 了。</p>
<p>​        这个其实没啥，但是我们却可以通过一个例子发现一个问题。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">const obj = &#123;</span><br><span class="line">  a: 1,</span><br><span class="line">&#125;</span><br><span class="line">obj.__proto__.x = 1</span><br><span class="line">console.log(x)</span><br></pre></td></tr></table></figure>

<p>​        看看这里，我先创建一个对象 obj，然后对其原型创建一个x的属性，然后我们如果直接访问x，则会发现，还真的可以访问。。</p>
<p>​        并不会报错。这里的原因又是为什么呢？</p>
<ul>
<li><p>简单来说就是，运行环境下，也是存在一个上下文的，存在上下文，那么便会有上层原型，JS里面所有的原型的最终的指向就是 Object.prototype 都会挨着挨着这样指上去。所以，一切的最终指向都是 顶层的Object的 prototype 。然后，就是运行环境的上下文问题了。</p>
<ul>
<li>```<br>const obj = {<br>  a: 1,<br>}<br>obj.<strong>proto</strong>.x = 1<br>console.log(x)<br>function c() {}<br>console.log(c.x)<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">* 运行环境的上下文，我们可以通过输出this，来知道上下文是什么，当然，输出一般都是一个空对象或者是window，但是这个并不是重点，重点是，为什么x，可以输出，因为他仅仅只是放在了obj的proto里面</span><br><span class="line"></span><br><span class="line">* 我们可以知道，JS 的原型最终都是指向的 Object.prototype 所以，obj.proto 其实也是指向的 这个，因为他这个是顶层的一个 new Object，然后我们修改了 proto 就是修改了 prototype，所以 JS 环境下的 prototype 就被修改了，然后我们访问 x，然后，在本地上下文不存在，于是就会去原型进行查找，this.proto 就是 prototype，所以这样的指向最后就是 prototype了。</span><br><span class="line"></span><br><span class="line">* ```</span><br><span class="line">  console.log(x, this.__proto__, Object.prototype)</span><br><span class="line">  this.__proto__ === Object.prototype ： true</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E9%9A%8F%E7%AC%94/" rel="tag"># 随笔</a>
              <a href="/tags/JavaScript/" rel="tag"># JavaScript</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/03/01/JavaScript/%E5%AF%B9%E8%B1%A1/Proxy%E5%92%8Cdefineproperty/" rel="prev" title="Proxy和defineproperty">
                  <i class="fa fa-chevron-left"></i> Proxy和defineproperty
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/03/05/JavaScript/ES6/JavaScript-ES6/" rel="next" title="JavaScript-ES6">
                  JavaScript-ES6 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tallgy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
